package com.example.spring.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.common.local.UserContextHolder;
import com.example.spring.demo.common.model.req.MyGoodsParam;
import com.example.spring.demo.entity.Goods;
import com.example.spring.demo.common.model.req.GoodsParam;
import com.example.spring.demo.common.model.vo.UpDownParam;
import com.example.spring.demo.common.model.req.WxIndexParam;
import com.example.spring.demo.service.GoodsService;
import com.example.spring.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/api/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private MessageService messageService;

    //发布
    @PostMapping("/release")
    public String release(@RequestBody Goods goods){
        Integer userId;
        try {
            userId = UserContextHolder.getContext().getUserId();
        } catch (Exception e) {
            userId = -1;
        }
        //设置时间
        goods.setCreateTime(new Date());
        //设置状态
        goods.setType("0");
        if(!goodsService.save(goods)){
            throw new CustomException("发布失败!");
        }
        messageService.saveUserAddGoods(userId, goods.getGoodsName());
        return "发布成功!";
    }
    //列表
    @GetMapping("/list")
    public IPage<Goods> getList(GoodsParam param){
        //构造分页对象
        IPage<Goods> page = new Page<>(param.getCurPage(),param.getPageSize());
        //构造查询条件
        QueryWrapper<Goods> query = new QueryWrapper<>();
        //商品名称
        if(StrUtil.isNotEmpty(param.getGoodsName())){
            query.lambda().like(Goods::getGoodsName,param.getGoodsName());
        }
        query.lambda().eq(Goods::getDeleteStatus,"0")
                .orderByDesc(Goods::getCreateTime);
        return goodsService.page(page, query);
    }
    //上架下架
    @PostMapping("/upanddown")
    public String upanddown(@RequestBody UpDownParam param){
        UpdateWrapper<Goods> query = new UpdateWrapper<>();
        query.lambda().set(Goods::getStatus,param.getStatus())
                .eq(Goods::getGoodsId,param.getGoodsId());
        if(!goodsService.update(query)){
            throw new CustomException("设置失败!");
        }
        return "设置成功!";
    }
    //推荐首页
    @PostMapping("/setIndex")
    public String setIndex(@RequestBody UpDownParam param){
        UpdateWrapper<Goods> query = new UpdateWrapper<>();
        query.lambda().set(Goods::getSetIndex,param.getSetIndex())
                .eq(Goods::getGoodsId,param.getGoodsId());
        if(goodsService.update(query)){
            return "设置成功!";
        }
        throw new CustomException("设置失败!");
    }
    //删除
    @PostMapping("/delete")
    public String delete(@RequestBody UpDownParam param){

        Integer userId;
        try {
            userId = UserContextHolder.getContext().getUserId();
        } catch (Exception e) {
            userId = -1;
        }

        UpdateWrapper<Goods> query = new UpdateWrapper<>();
        query.lambda().set(Goods::getDeleteStatus,"1")
                .eq(Goods::getGoodsId,param.getGoodsId());
        if(goodsService.update(query)){
            messageService.saveUserDelGoods(userId, param.getGoodsId());
            return "删除成功!";
        }
        throw new CustomException("删除失败!");
    }

    //小程序首页推荐列表
    @GetMapping("/getIndexList")
    public IPage<Goods> getIndexList(WxIndexParam param){
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.lambda().like(
                StrUtil.isNotEmpty(param.getKeywords()),Goods::getGoodsName,param.getKeywords())
                .eq(Goods::getSetIndex,"1")//0:未推荐 1：推荐到首页
                .eq(Goods::getStatus,"0")
                .orderByDesc(Goods::getCreateTime);
        IPage<Goods> page = new Page<>(param.getCurPage(),param.getPageSize());
        return goodsService.page(page,query);
    }

    //小程序闲置列表查询
    @GetMapping("/getUsedList")
    public IPage<Goods> getUsedList(WxIndexParam param){
        //构造查询条件
        QueryWrapper<Goods> query = new QueryWrapper<>();
        //判断分类
        if(param.getCategoryId()==null || param.getCategoryId()==0){
            query.lambda().like(StrUtil.isNotEmpty(param.getKeywords()),Goods::getGoodsName,param.getKeywords())
                    .eq(Goods::getStatus,"0")
                    .eq(Goods::getDeleteStatus,"0")
                    // .eq(Goods::getType,"0")
                    .eq(Goods::getSellStatus,"0")
                    .orderByDesc(Goods::getCreateTime);
        }else{
            //通过分类ID获取数据
            query.lambda().like(StrUtil.isNotEmpty(param.getKeywords()),Goods::getGoodsName,param.getKeywords())
                    .eq(Goods::getStatus,"0")
                    .eq(Goods::getDeleteStatus,"0")
                    // .eq(Goods::getType,"0")
                    .eq(Goods::getSellStatus,"0")
                    .eq(Goods::getCategoryId,param.getCategoryId())
                    .orderByDesc(Goods::getCreateTime);
        }
        //构造分页对象
        IPage<Goods> page = new Page<>(param.getCurPage(),param.getPageSize());
        return goodsService.page(page, query);
    }

    //小程序我发布的闲置
    @GetMapping("/getMyUnusedList")
    public IPage<Goods> getMyUnusedList(MyGoodsParam param){
        //构造查询条件
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.lambda().eq(Goods::getUserId,param.getUserId())
                .eq(Goods::getType,param.getType())
                .eq(Goods::getDeleteStatus,"0");
        //构造分页对象
        IPage<Goods> page = new Page<>(param.getCurPage(),param.getPageSize());
        return goodsService.page(page, query);
    }

    //编辑
    @PostMapping("/edit")
    public String edit(@RequestBody Goods goods){
        goods.setType("0");
        if(goodsService.updateById(goods)){
            return "编辑成功！";
        }
        throw new CustomException("编辑失败!");
    }

    //小程序我的订单
    @GetMapping("/getMyOrder")
    public IPage<Goods> getMyOrder(MyGoodsParam param){
        return goodsService.getMyOrder(param);
    }
    //我的出售订单
    @GetMapping("/getSellOrder")
    public IPage<Goods> getSellOrder(MyGoodsParam param){
        return goodsService.getSellOrder(param);
    }

}
